using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ComputerShopE3.Models;

namespace ComputerShopE3.Controllers
{
    public class CompanyInfoController : Controller
    {
        ComputerShopDBEntities1 _db = new ComputerShopDBEntities1();
        //
        // GET: /CompanyInfo/

        public ActionResult Index()
        {
            //tra ve entry cuoi cung
            var kq = (from tt in _db.THONG_TIN
                      orderby tt.id descending
                      select tt).First();            
            return View(kq);
        }

        //
        // GET: /CompanyInfo/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /CompanyInfo/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /CompanyInfo/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /CompanyInfo/Edit/5
 
        public ActionResult Edit(int id)
        {            
            return View(_db.THONG_TIN.Where(tt=>tt.id == id).First());
        }

        //
        // POST: /CompanyInfo/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(THONG_TIN ttToEdit)
        {
            try
            {
                var originalTT = _db.THONG_TIN.Where(tt => tt.id == ttToEdit.id).First();
                if (!ModelState.IsValid)
                    return View(originalTT);
                _db.ApplyPropertyChanges(originalTT.EntityKey.EntitySetName, ttToEdit);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
